package com.corder.display.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailHelper {
	 private String server;       //���������

	    public MailHelper(String server) {
	        this.server = server;      
	    }

	    public void send(String to_mail, String from_mail, String to_title, String to_content) {
	        try {

	            //----�����ʼ��������----
	            //String to_contents = codeToString(to_content);        
	            //String to_titles = codeToString(to_title);     
	            Properties props = new Properties();
	            //------�����ʼ��Ự------
	            props.put("mail.smtp.host", server);//smtp.163.com
	            props.put("mail.smtp.auth", "true");
	            props.put("mail.smtp.port", 25);
	            
	            Session s = Session.getInstance(props);
	            s.setDebug(true);
	            MimeMessage message = new MimeMessage(s);
	            //--����Ϣ�������÷����ˡ��ռ��ˡ����⡢����ʱ��--
	            InternetAddress from = new InternetAddress(from_mail);
	            message.setFrom(from);//������
	            InternetAddress to = new InternetAddress(to_mail);
	            message.setRecipient(Message.RecipientType.TO, to);
	            message.setSubject(to_title);
	            message.setSentDate(new Date());
	            BodyPart mdp = new MimeBodyPart();
	            //��BodyPart�����������ݺ͸�ʽ�ı��뷽ʽ
	            mdp.setContent(to_content, "text/html;charset=utf-8");
	            //�½�һ��MimeMultipart�����������BodyPart����(��ʵ�Ͽ��Դ�Ŷ��)
	            Multipart mm = new MimeMultipart();
	            //��BodyPart���뵽MimeMultipart������(���Լ�����BodyPart)
	            mm.addBodyPart(mdp);
	            //��mm��Ϊ��Ϣ���������
	            message.setContent(mm);
	            message.saveChanges();            
	            Transport.send(message);
	        } catch (MessagingException e) {
	            e.printStackTrace();
	            System.out.println("�����ʼ�ʧ��!");
	        }
	    }

	    //���������ַ�ĺ���
	    public String codeToString(String str) {
	        String s = str;
	        try {
	            byte tempB[] = s.getBytes("ISO-8859-1");
	            s = new String(tempB);
	            return s;
	        } catch (Exception e) {
	            return s;
	        }
	    } 
	    
	    public static void main(String[] args) {
	    	 StringBuilder content = new StringBuilder();
	         content.append("DefensorȨ�޹���ϵͳ");
	         content.append("</br>----------------------");
	         content.append("</br>�û���:").append("�Ʋ�");
	         content.append("</br>��֤��:").append("62233");
	         content.append("</br>----------------------");
	         content.append("<div id=\"post_content_1483487239\" class=\"d_post_content j_d_post_content \">�q�Шr�衡����������&nbsp;\n"
	                 + "<br>�q�q&nbsp;�Шr&nbsp;��q��r��&nbsp;\n"
	                 + "<br>�t&nbsp;----�s/���Ũ�\\&nbsp;��&nbsp;\n"
	                 + "<br>~~~~~~~~~~��~~��~~~~~~~~~~~.</div>");
	         MailHelper mail = new MailHelper("smtp.163.com");
	         mail.send("120158285@qq.com", "qwer120158285@163.com", "��ĵ�¼��֤" + new Date().toString(), content.toString());
		}
}
